package com.chat.core.controller;


import com.chat.core.entity.User;
import com.chat.core.service.UserService;
import com.chat.core.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

/**
 * 用户(User)表控制层
 *
 * @author makejava
 * @since 2022-04-11 13:43:48
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;


    @PostMapping("login")
    public Result login(@RequestParam("username") String username,@RequestParam("password") String password) throws IOException {
        return Result.ok(WsSessionManager.getNum().toString(),userService.login(username, password));
    }

    /**
     * 获取在线用户列表
     * @return
     */
    @GetMapping("online")
    public Result getOnlineUserList(){
        return Result.ok(userService.getOnlineList());
    }

    /**
     * 获取聊天列表
     * @return
     */
    @GetMapping("chat_list/{id}")
    public Result getChatUser(@PathVariable Integer id){ return Result.ok(userService.getChatUserList(id)); }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return Result.ok(this.userService.getById(id));
    }

    /**
     * 用户注册
     *
     * @param user 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody User user) {
        return Result.ok(this.userService.register(user));
    }

    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody User user) {
        return Result.ok(this.userService.updateUser(user));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.ok(this.userService.removeByIds(idList));
    }
}
